ko.extensions.webos.view = function(view)
{
    switch(view)
    {
        case "command":
            ko.extensions.webos.view.panelBottom = "webosCommand";
            view = "main";
            break;
        case "todo":
            ko.extensions.webos.todo.project();
            ko.extensions.webos.view.panelBottom = "webosToDo";
            view = "main";
            break;
        case "jslint":
            ko.extensions.webos.jslint.project();
            ko.extensions.webos.view.panelBottom = "webosJSLint";
            view = "main";
            break;
        case "newproject":
            var uri = "chrome://webos/content/newproject.json";
            var id = "webosNewApplicationScroll";
            ko.extensions.webos.json.createEditArea(id, uri);
            ko.extensions.webos.view.panelBottom = "webosNewApplication";
            view = "main";
            break;
        case "preware":
            ko.extensions.webos.view.panelBottom = "webosPreware";
            view = "main";
            break;
        case "editinfopanel":
            ko.extensions.webos.project.updateEditInfoField();
            ko.extensions.webos.view.panelBottom = "webosEditInfo";
            view = "main";
            break;
        default:
            ko.extensions.webos.view.panelBottom = "webosCommand";
    }
    switch(view)
    {
        case "debug":
            if(document.getElementById("webosDebug").style.display == "none")
            {
                document.getElementById("webosDebug").style.display = "-moz-box";
                document.getElementById("webosDebugButton").setAttribute("image", "chrome://webos/skin/bug_arrowright.png");
            }
            else
            {
                document.getElementById("webosDebug").style.display = "none";
                document.getElementById("webosDebugButton").setAttribute("image", "chrome://webos/skin/bug_arrowleft.png");
            }
            break;
        case "editinfo":
            if(document.getElementById("webosEditInfoView").style.display == "none")
            {
                // Place values in quick edit version fields
                ko.extensions.webos.project.updateVersionField();
                document.getElementById("webosEditInfoView").style.display = "-moz-box";
                document.getElementById("webosEditInfoButton").setAttribute("image", "chrome://webos/skin/page_white_text_edit_arrowleft.png");
            }
            else
            {
                document.getElementById("webosEditInfoView").style.display = "none";
                document.getElementById("webosEditInfoButton").setAttribute("image", "chrome://webos/skin/page_white_text_edit_arrowright.png");
            }
            break;
        case "addscene":
            if(document.getElementById("webosAddSceneView").style.display == "none")
            {
                document.getElementById("webosFileNewSceneMenuItem").disabled = true;
                document.getElementById("webosFileNewSceneMenuItem").setAttribute("image", "chrome://webos/skin/page_white_add_disabled.png");
                document.getElementById("webosAddSceneView").style.display = "-moz-box";
                document.getElementById("webosEditInfoButton").setAttribute("image", "chrome://webos/skin/page_white_text_edit_arrowleft.png");
                document.getElementById("webosAddSceneText").focus();
            }
            else
            {
                document.getElementById("webosFileNewSceneMenuItem").setAttribute("image", "chrome://webos/skin/page_white_add.png");
                document.getElementById("webosFileNewSceneMenuItem").disabled = false;
                document.getElementById("webosAddSceneView").style.display = "none";
                document.getElementById("webosEditInfoButton").setAttribute("image", "chrome://webos/skin/page_white_text_edit_arrowright.png");
                document.getElementById("webosAddSceneText").value = "";
            }
            break;
        case "main":
            if(ko.extensions.webos.view.panelBottom == "webosNewApplication"
               || ko.extensions.webos.view.panelBottom == "webosEditInfo")
                document.getElementById("webosMain").style.display = "none";
            else
                document.getElementById("webosMain").style.display = "-moz-box";
            var e = ["webosNewApplication",
                     "webosEditInfo",
                     "webosEditInfoView",
                     "webosDebug",
                     "webosCommand",
                     "webosJSLint",
                     "webosToDo",
                     "webosDisabled"];
            for(var i in e)
                document.getElementById(e[i]).style.display = "none";
            if(ko.extensions.webos.project.isWebOS || ko.extensions.webos.view.panelBottom == "webosNewApplication")
            {
                document.getElementById("webosDebugButton").
                    setAttribute("image", "chrome://webos/skin/bug_arrowleft.png");
                document.getElementById("webosEditInfoButton").
                    setAttribute("image", "chrome://webos/skin/page_white_text_edit_arrowright.png");
                document.getElementById(ko.extensions.webos.view.panelBottom).style.display = "-moz-box";
            }
            else
            {
                document.getElementById("webosDebugButton").
                    setAttribute("image", "chrome://webos/skin/bug_arrowleft_disabled.png");
                document.getElementById("webosEditInfoButton").
                    setAttribute("image", "chrome://webos/skin/page_white_text_edit_arrowright_disabled.png");
                document.getElementById("webosDisabled").style.display = "-moz-box";
            }
            break;
        default:
            alert('unknown view');
    }
    switch(view)
    {
        case "webosEditInfo":
            ko.extensions.webos.project.updateEditInfoField();
            document.getElementById("webosEditInfoView").style.display = "none";
            document.getElementById("webosEditInfoButton").setAttribute("image", "chrome://webos/skin/page_white_text_edit_arrowright.png");
            break;
    }
    ko.extensions.webos.view.resize();
};

ko.extensions.webos.view.panelBottom = "webosCommand";

ko.extensions.webos.view.url = function(url)
{
    var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance();
    messenger = messenger.QueryInterface(Components.interfaces.nsIMessenger);
    messenger.launchExternalURL(url);
};

ko.extensions.webos.view.resize = function()
{
    
    var h = document.getElementById("webosTabPanel").boxObject.height;
    document.getElementById("webosNewApplicationScroll").style.overflow = "auto";
    document.getElementById("webosNewApplicationScroll").style.height = (h - 30) + "px";
    document.getElementById("webosEditInfoScroll").style.overflow = "auto";
    document.getElementById("webosEditInfoScroll").style.height = (h - 30) + "px";
};

ko.extensions.webos.view.userInterface = function(enabled)
{
    ko.extensions.webos.view("main");
    var e = ["webosEmulatorPackageLaunchInstallButton",
             "webosEmulatorInspectButton",
             "webosEmulatorRemoveButton",
             "webosEmulatorLaunchButton",
             "webosEmulatorCloseButton",
             "webosDevicePackageInstallLaunchButton",
             "webosDeviceRemoveButton",
             "webosDeviceLaunchButton",
             "webosDeviceCloseButton",
             "webosEditInfoButton",
             "webosDebugButton",
             "webosJSLintButton",
             "webosCommandLineButton",
             "webosToDoButton",
             //"webosBeautifierButton",
             "webosFileNewSceneMenuItem"];
    var enable = ["box_add.png",
                  "box_magnifier.png",
                  "box_delete.png",
                  "box_go.png",
                  "box_cancel.png",
                  "pre_add.png",
                  "pre_delete.png",
                  "pre_go.png",
                  "pre_cancel.png",
                  "page_white_text_edit_arrowright.png",
                  "bug_arrowleft.png",
                  "script_gear.png",
                  "cog_textfield.png",
                  "clipboard.png",
                  //"script_palette.png",
                  "page_white_add.png"];
    var disable = ["box_add_disabled.png",
                   "box_magnifier_disabled.png",
                   "box_delete_disabled.png",
                   "box_go_disabled.png",
                   "box_cancel_disabled.png",
                   "pre_add_disabled.png",
                   "pre_delete_disabled.png",
                   "pre_go_disabled.png",
                   "pre_cancel_disabled.png",
                   "page_white_text_edit_arrowright_disabled.png",
                   "bug_arrowleft_disabled.png",
                   "script_gear_disabled.png",
                   "cog_textfield_disabled.png",
                   "clipboard_disabled.png",
                   //"script_palette_disabled.png",
                   "page_white_add_disabled.png"];
    var eElement = ["webosConfigButton"];
    var eImage = ["webosConfigImage"];
    var enableImage = ["cog_off_arrowdown.png"];
    var disableImage = ["cog_off_arrowdown_disabled.png"];
    for(var i in e)
    {
        var ele = document.getElementById(e[i]);
        if(enabled)
            ele.setAttribute("image", "chrome://webos/skin/" + enable[i]);
        else
            ele.setAttribute("image", "chrome://webos/skin/" + disable[i]);
        ele.disabled = !enabled;
    }
    for(var i in eElement)
    {
        var ele = document.getElementById(eElement[i]);
        ele.disabled = !enabled;
    }
    for(var i in eImage)
    {
        var ele = document.getElementById(eImage[i]);
        if(enabled)
            ele.setAttribute("src", "chrome://webos/skin/" + enableImage[i]);
        else
            ele.setAttribute("src", "chrome://webos/skin/" + disableImage[i]);
    }
    if(enabled)
        ko.extensions.webos.command.iconStatic();
    else
        ko.extensions.webos.command.iconDisabled();
    document.getElementById("webosAddSceneView").style.display = "none";
};
ko.extensions.webos.view.webosBlog = function()
{
    ko.browse.openUrlInDefaultBrowser("http://webos.templarian.com/");
};
ko.extensions.webos.view.download = function()
{
    ko.browse.openUrlInDefaultBrowser("http://webos.templarian.com/komodo/");
};
ko.extensions.webos.view.donate = function()
{
    ko.browse.openUrlInDefaultBrowser("https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=JAJEKK28BB6EQ&lc=US&item_number=webos&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted");
};
ko.extensions.webos.view.bugs = function()
{
    ko.browse.openUrlInDefaultBrowser("http://bug.webos.templarian.com/");
};